var service_view = require("../../service/view.js");
var service_user = require("../../service/user.js");
var allUserList = [];
var searchUserList2 = [];
var page = 1;//名片夹的开始数
var value = "";
var shouquan = null;
Page({
  data: {
    searchUserList: {},
  },
  onLoad: function () {
    page = 1;
    allUserList = [];
    searchUserList2 = [];
    service_view.getCollectList(page, this.showData);
    wx.showToast({
      title: '加载数据中',
      icon: 'loading',
      duration: 100000
    })
  },
  onPullDownRefresh: function () {
    allUserList = [];
    searchUserList2 = [];
    page = 1;
    service_view.getCollectList(page, this.showData);
  },
  //自定义显示
  showData: function (res) {
    var dataList = res.data.data.data;
    for (var i = 0; i < dataList.length; i++) {
      allUserList.push(dataList[i]);
    }
    page++;
    //更新数据
    this.setData({
      searchUserList: allUserList
    })
    wx.hideToast()
    wx.stopPullDownRefresh();
  },
  allList: function (res) {
    allUserList = res.data.data;
    console.log(res.data.data)
    searchUserList2 = [];
    if (value == "") {
      searchUserList2 = allUserList;
    } else {
      for (var i = 0; i < allUserList.length; i++) {
        var nickname = allUserList[i].nickname
        var company = allUserList[i].company
        if (nickname != '' && company != '' && nickname != 'null' && company != 'null') {
          if (nickname.indexOf(value) != -1) {
            searchUserList2.push(allUserList[i]);
          }
          else if (company.indexOf(value) != -1) {
            searchUserList2.push(allUserList[i]);
          }
        }
      }
    }
    //更新数据
    this.setData({
      searchUserList: searchUserList2
    })
    wx.hideToast();
  },
  //查找名片
  searchData: function (e) {
    wx.showToast({
      title: '加载数据中',
      icon: 'loading',
      duration: 100000
    });
    allUserList = [];
    value = e.detail.value;
    service_view.getCollectList(page, this.allList, 0);
  },
  //点击查看名片
  viewCard: function (e) {
    add_form_id(e.detail.formId)
    console.log(e);
    var openid = e.currentTarget.dataset.openid;
    wx.navigateTo({
      url: '/pages/other/other?openid=' + openid,
    })
  },

  //打电话
  callPhone: function (event) {
    var phone = event.target.dataset.phone;
    if (phone != "") {
      wx.makePhoneCall({
        phoneNumber: phone,
        success: function (res) {
          // success
        }
      })
    }
  },
  //上拉加载
  onReachBottom: function () {
    wx.showToast({
      title: '加载数据中',
      icon: 'loading',
      duration: 100000
    });
    service_view.getCollectList(page, this.showData);
  },
})
//添加form_id
function add_form_id(form_id) {
service_user.addFormId(form_id);
}